Ext.define('AmpleHarvest.util.solrFacets', {
    extend		: 'Ext.util.Observable',
    fixedFilter	: false,
//    str_type	: 'terms', // alternatly send in type 'facet'
    getData: function (str_entity, str_prefix, str_target_url) {
		obj_params = new Object();
    	
		if(!Ext.isBoolean(this.fixedFilter))
		{
			if(str_prefix == null)
			{
				str_prefix = this.fixedFilter;
			}
		}
		
		if(this.str_type == 'terms')
    	{
    		str_target_url =  str_target_url+'terms';
    		
    		obj_params[this.str_type+'.fl'] 	= str_entity;
    		obj_params['json.wrf'] 				= 'this.solr_callback';
    	} 
    	else if (this.str_type == 'facet')
    	{
    		this.str_entity = str_entity;
    		str_target_url =  str_target_url+'select';
    		obj_params['q'] 					= '*:*';
    		obj_params['facet'] 				= 'true';
    		obj_params[this.str_type+'.field'] 	= str_entity;
    		obj_params['rows'] 					= 0;
    		obj_params['json.wrf'] 				= 'this.solr_callback'
    	} 
//    	else {
//    		console.log('you must send in str_type of terms or facet');
//    		return false;
//    	}
    	
    	obj_params[this.str_type+'.limit'] 	= 400,
		obj_params[this.str_type+'.sort'] 	= 'index',
		obj_params[this.str_type+'.prefix'] = str_prefix,
    	obj_params['wt'] 					= 'json',
		obj_params['json.nl'] 				= 'arrarr',

//    	console.log(str_target_url);
//    	console.log(obj_params);
        Ext.Ajax.request({
            url: str_target_url+'?',
            method: 'GET',
            params: obj_params,
            timeout: 100000,
            scope: this,
            success: function (response) {
//                try {
                	this.str_entity = str_entity;
                	eval(response.responseText);
//                } catch (e) {
//                    console.log(e);
//                }
            },
            failure: function (e) {
                console.log('Ajax Error:'+e);
            }
        });
    },
    
    solr_callback	: function (obj)
    {
    	if(this.str_type == 'facet')
    	{
    		var obj_facets = obj.facet_counts.facet_fields[this.str_entity];
    	} else {
    		console.log(obj);
    		if(Ext.isArray(obj.terms))
    		{
    			var obj_facets = obj.terms[0][1];
    		} else {
    			var obj_facets = obj.terms[this.str_entity];
    		}
    		
    	}
    	
    	var arr_terms = new Array();
    	for(v in obj_facets)
    	{
    		arr_terms.push({
    			label	: obj_facets[v][0]+'('+obj_facets[v][1]+')',
    			term	: obj_facets[v][0],
    			count	: obj_facets[v][1]
    		})
    	}
    	console.log(arr_terms);
    	this.AHcaller.fireEvent('loadData', arr_terms);
    }
});